package bp.sys;

import bp.cloud.Orgs;
import bp.da.DataType;
import bp.en.*;
import bp.web.WebUser;

public class AutoTask  extends EntityMyPK {

    public  UAC getHisUAC()
    {
            UAC uac = new UAC();
            uac.Readonly();
            return uac;
    }

    public String getTaskName()
    {
        return this.GetValStringByKey("TaskName");

    }
    public String getTaskModel()
    {

            return this.GetValStringByKey("TaskModel");

    }
    public String getDocs()
    {

            return this.GetValStringByKey("Docs");

    }
    public String getTimes()
    {

            return this.GetValStringByKey("Times");

    }
    public String getDBSrc()
    {

            return this.GetValStringByKey("DBSrc");

    }
    /// <summary>
    /// 自动任务
    /// </summary>
    public AutoTask()
    {
    }

    @Override
    public bp.en.Map getEnMap() {

            if (this.get_enMap() != null)
            {
                return this.get_enMap();
            }
            Map map = new Map("Sys_AutoTask", "自动任务");

            map.AddMyPK();
            map.AddTBString("TaskName", null, "名称", true, true, 0, 200, 200);
            map.AddTBString("TaskModel", null, "内容类型", true, true, 0, 200, 100);
            map.AddTBStringDoc("Docs", null, "执行内容", true, true, true);
            map.AddTBString("Times", null, "执行时间", true, true, 0, 200, 300);
            map.AddTBString("DBSrc", null, "数据源", true, true, 0, 200, 50);
            map.AddTBStringDoc("NoteMark", null, "说明", true, true, true);
            map.AddTBAtParas(500);

            this.set_enMap(map);
            return this.get_enMap();
    }


    public String RunTask() throws Exception {
        if (this.getTaskModel().equals("SQL") == true)
        {
            String sql = this.getDocs();
            sql = sql.replace("@WebUser.No", WebUser.getNo());
            sql = sql.replace("@WebUser.Name", WebUser.getName());
            sql = sql.replace("@WebUser.DeptNo", WebUser.getDeptNo());
            sql = sql.replace("@WebUser.OrgNo", WebUser.getOrgNo());
            if (DataType.IsNullOrEmpty(this.getDBSrc()) == true || this.getDBSrc().equals("DBSrc"))
            {
                this.SetValByKey("DBSrc", "local");
            }
            SFDBSrc sFDBSrc = new SFDBSrc();
            int i = sFDBSrc.RunSQL(sql);
            return "执行成功:影响" + i + "条.";
        }

        if (this.getTaskModel().equals("BU") == true)
        {

            BuessUnitBase enBuesss = bp.sys.base.Glo.GetBuessUnitEntityByEnName(this.getDocs());
            String doc = enBuesss.DoIt();
            return doc;
        }

        //@hongyan
        if (this.getTaskModel().equals("Method") == true)
        {
            Method rm = bp.en.ClassFactory.GetMethod(this.getDocs());
            rm.Init();
            Object obj = rm.Do();
            return obj.toString();
        }

        return "没有判断的类型:"+this.getTaskModel();
    }

    @Override
    public bp.en.Entities GetNewEntities()
    {
        return new AutoTasks();
    }
}
